package com.jqq.api.transaction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author 姬倩倩
 * @version 1.0
 * bank的数据库操作方法存储类
 */
public class BankDao {

    /**
     * 加钱的数据库操作方法(jdbc)
     * @param account 加钱的行号
     * @param money 加钱的金额
     */
    public void add(String account, int money, Connection connection) throws Exception {
        //3.编写SQL语句
        String sql = "update t_bank set money = money + ? where account = ?;";
        //4.创建preparedStatement并为占位符赋值
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1, money);
        preparedStatement.setObject(2, account);
        //5.发送SQL语句
        preparedStatement.executeUpdate();
        //6.关闭资源
        preparedStatement.close();
        System.out.println("加钱成功！");

    }

    /**
     * 减钱的数据库操作方法(jdbc)
     * @param account 减钱的行号
     * @param money 减钱的金额
     */
    public void sub(String account, int money, Connection connection) throws Exception {

        //3.编写SQL语句
        String sql = "update t_bank set money = money - ? where account = ?;";
        //4.创建preparedStatement并为占位符赋值
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1, money);
        preparedStatement.setObject(2, account);
        //5.发送SQL语句
        preparedStatement.executeUpdate();
        //6.关闭资源
        preparedStatement.close();
        System.out.println("减钱成功！");

    }
}
